public class SingleLinkedList {


    static class LinkNode{
        public int val;
        public LinkNode next;

        public LinkNode(int val){
            this.val = val;
        }
    }

    public LinkNode head;

    public void createLinkedList(){
        LinkNode node1 = new LinkNode(1);
        LinkNode node2 = new LinkNode(1);
        LinkNode node3 = new LinkNode(1);
        LinkNode node4 = new LinkNode(1);
        LinkNode node5 = new LinkNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;

    }

    //头插法
    public void addFirst(int data){
        LinkNode node = new LinkNode(data);
        if(head == null){
            head = node;
            return;
        }
        node.next = head;
        head = node;



    }

    //尾插法
    public void addLast(int data){
        LinkNode node = new LinkNode(data);
        if(head == null){
            head = node;
            return;
        }
        LinkNode cur = head;
        //cur走到最后一个节点
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > size()){
            System.out.println("index位置不合法");
            return;
        }

        LinkNode node = new LinkNode(data);

        if(index == 0){
            node.next = head;
            head = node;
            return;
        }

        LinkNode cur = head;
        if(index == size()){
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
            return;
        }

        for(int i = 0; i < index - 1; i++){
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        LinkNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        }

        if(head.val == key){
            head = head.next;
            return;
        }
        LinkNode prev = head;
        LinkNode cur = head;


        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                return;
            }
            else{
                prev = cur;
                cur = cur.next;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        LinkNode prev = head;
        LinkNode cur = head.next;
        while(cur != null){

            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }

        }
        if(head.val == key){
            head = head.next;
        }
    }

    //得到单链表的长度
    public int size(){
        LinkNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //清除单链表
    public void clear() {
        LinkNode cur = head;

        while(cur != null){
            LinkNode curN = cur.next;
            cur.next = null;
            cur = curN;

        }
        head = null;


    }

    //打印单链表
    public void display() {
        LinkNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        SingleLinkedList linkedList = new SingleLinkedList();
        linkedList.createLinkedList();
        linkedList.display();
        linkedList.removeAllKey(1);
        linkedList.display();
        linkedList.clear();
        linkedList.display();
    }

    public static void main1(String[] args) {
        SingleLinkedList linkedList = new SingleLinkedList();
        linkedList.createLinkedList();
        linkedList.display();
        System.out.println(linkedList.size());
        System.out.println(linkedList.contains(8));
        System.out.println("头插法");
        linkedList.addFirst(6);
        linkedList.addFirst(7);
        linkedList.addFirst(8);
        linkedList.display();
        System.out.println("尾插法");
        linkedList.addLast(10);
        linkedList.addLast(10);
        linkedList.addLast(10);
        linkedList.display();
        linkedList.remove(6);
        linkedList.display();

    }

}
